import React, { Component } from 'react'
import { browserHistory } from 'react-router';
import { TopHeader, SideAntd, Footer } from '../../components'
import Actions from '../../actions';
import Welcome from '../Welcome';
import cookie from 'react-cookie';
import { Tabs, Icon } from 'antd';
import {Modal} from 'antd';

import $ from 'jquery'

const TabPane = Tabs.TabPane;


export default class Main extends Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {

        
        var urlUserName=this.GetQueryString("userName");
        var urlUserPassword=this.GetQueryString("userPassword");

        if (urlUserName === "" || urlUserName === null) {
            Modal.info({
                title: "提示",
                content: "账号不能为空"
            });
        } else if (urlUserPassword === "" || urlUserPassword === null) {
            Modal.info({
                title: "提示",
                content: "密码不能为空"
            });
        } else {
            this.setState({
                loginLoading: '登录中'
            });
            window.localStorage.setItem('gongc','')
            let _this=this;
            $.ajax({
                url: '/base/auth/login',
                type: 'post',
                dataType: 'json',
                data: {
                    username: urlUserName,
                    password: urlUserPassword
                }
            })
            .done(function (data, status, xhr) {
                //console.log("----------",  JSON.stringify(data, null, 4));
                if (data.code == "1") {
                    if(data.data=='pwd'){
                        this.showEditPwdHandle(_this);

                    }else{
                        this.setState({
                            loginLoading: '登录'
                        });
                        this.fetchPerms();
                        // window.isLogin = true;
                        browserHistory.push('/main');
                    }
                } else {
                    console.log("失败：", data.msg);

                    this.setState({
                        loginLoading: '登录'
                    });
                    Modal.error({
                        title: "登录错误",
                        content: data.msg
                    });
                }
            }.bind(this));
        }
    }
   
    GetQueryString = (name) =>{
         var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var r = window.location.search.substr(1).match(reg);
         if(r!=null)return  unescape(r[2]); return null;
    }

    //获取操作权限
    fetchPerms = () => {
        window.localStorage.setItem('permissions','');
        $.ajax({
            url: ' /base/auth/perms',
            type: 'post',
            dataType: 'json',
            data: {}
        }).done(function (data, status, xhr) {
                if (data.code == "1") {
                    window.localStorage.setItem('permissions',data.data.permissions);
                } else {
                    console.log("失败：", data.msg);
                }
            }.bind(this));
    }

    handleClick = () => {
        //提交表单 成功后进行跳转
    };

    render() {

        return(
            <div >
              
            </div>
        )
    }
}
